import {Component, OnInit, ViewChild} from '@angular/core';
import {adSystemApiUrl, AliyunOssOutputUrl} from 'config/api';
import {Router} from '@angular/router';
import {HttpService} from 'app/lib/service/http.service';
import {AdOssUploadService} from 'app/lib/service/ad-oss-upload.service';
import {LocalStorageService} from '../../../lib/service/local-storage.service';
import {AddArticle} from '../article-manage';
import {EditorComponent} from "../../../comm-components/wangeditor/editor.component";

@Component({
  selector: 'app-add-place',
  templateUrl: 'add.component.html',
  styleUrls: ['add.component.scss'],
  providers: [HttpService]
})
export class AddComponent implements OnInit {

  addArticle = new AddArticle();
  aliyunOssOutputUrl: string = AliyunOssOutputUrl;
  classifyList: any;
  hasUpload = true;
  image: string;

  type = 'success';
  message = '添加成功';
  isShow = false;
  img_file: any;
  img_name: string;
  img_url: string;
  ewm_file: any;
  ewm_name: string;
  ewm_url: string;

  @ViewChild(EditorComponent) editor: EditorComponent;
  constructor( private httpService: HttpService,
                private adOssUploadService: AdOssUploadService,
                private localStorageService: LocalStorageService,
                private router: Router) {
  }

  ngOnInit() {
    this.addArticle.author_id = this.localStorageService.getAdminInfo().id;
    this.getClassifyList();
    this.editor.changedEitorUsePlace('article');
  }

  // 获取分类列表
  getClassifyList() {
    const url = `${adSystemApiUrl}/v1/admin/category/list`;
    return this.httpService.httpGetAuth(url).subscribe(
      data => {
        this.classifyList = data.data;
        this.addArticle.category_id = data.data[0].id;
      }
    );
  }

  // 上传图片
  uploadImg($event: any, imgName: string) {
    const file = $event.path[0].files[0];
    const myReader = new FileReader();
    const self = this;
    const suffix = file.name.replace(/.+\./, '').toLowerCase();
    this.img_file = file;
    this.img_name = imgName + '.' + suffix;
    myReader.readAsDataURL(file);
    myReader.onloadend = function (e) {
      self.img_url = myReader.result;
    };
  }

  // 提交表单
  submitForm() {
    if (!this.img_file) {
      this.hasUpload = false;
      return;
    }
    this.addArticle.image = this.img_name;
    this.addArticle.body = this.editor.clickHandle();
    if (!this.addArticle.body) {
      this.type = 'info';
      this.message = '请输入文章内容';
      this.isShow = true;
      return;
    }
    const url = `${adSystemApiUrl}/v1/admin/posts/add`;
    return this.httpService.httpPostAuth(url, this.addArticle).subscribe(
      res => {
        if (res.code === 2000) {
          this.adOssUploadService.getSignature('/v1/common/post/' + res.data.id + '?callback=true').subscribe(
            res1 => {
              this.adOssUploadService.fileUpload(this.img_file, res1.data, this.img_name, 'image').subscribe(
                res2 => {
                  if (res2.status === 200) {
                    this.type = 'success';
                    this.message = '添加成功';
                    this.isShow = true;
                    this.router.navigate(['admin/article-manage/article-list']);
                  } else {
                    this.type = 'error';
                    this.message = '图片上传失败';
                    this.isShow = true;
                  }
                }
              );
            }
          );
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    );
  }
    PostData(event): void {
      console.log("1");
      console.log(event);
    }
}
